package com.lingnan.controller;


import com.lingnan.dto.ResumeOptimizeRequest;
import com.lingnan.dto.ResumeOptimizeResponse;

import com.lingnan.entity.Result;
import com.lingnan.service.ResumeOptimizeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/ai/resume")
@RequiredArgsConstructor
public class ResumeOptimizeController {

    private final ResumeOptimizeService resumeOptimizeService;

    /**
     * 优化简历，除了简历id，其他参数都可以不填，默认使用简历的职位和行业
     */
    @PostMapping("/optimize")
    public Result<ResumeOptimizeResponse> optimizeResume(@RequestBody ResumeOptimizeRequest request) {
        try {
            ResumeOptimizeResponse response = resumeOptimizeService.optimizeResume(request);
            return Result.ok(response);
        } catch (Exception e) {
            log.error("简历优化失败", e);
            return Result.fail("简历优化失败：" + e.getMessage());
        }
    }

    /**
     * 获取优化历史记录
     */
    @GetMapping("/optimize/history")
    public Result<List<ResumeOptimizeResponse>> getOptimizeHistory(
            @RequestParam("resumeId") Long resumeId) {
        try {
            List<ResumeOptimizeResponse> history = resumeOptimizeService.getOptimizeHistory(resumeId);
            return Result.ok(history);
        } catch (Exception e) {
            log.error("获取优化历史失败", e);
            return Result.fail("获取优化历史失败：" + e.getMessage());
        }
    }

    /**
     * 获取最新优化记录
     */
    @GetMapping("/optimize/latest")
    public Result<ResumeOptimizeResponse> getLatestOptimize(
            @RequestParam("resumeId") Long resumeId) {
        try {
            ResumeOptimizeResponse latest = resumeOptimizeService.getLatestOptimize(resumeId);
            return Result.ok(latest);
        } catch (Exception e) {
            log.error("获取最新优化记录失败", e);
            return Result.fail("获取最新优化记录失败：" + e.getMessage());
        }
    }

    /**
     * 重新优化简历
     */
    @PostMapping("/re-optimize")
    public Result<ResumeOptimizeResponse> reOptimize(
            @RequestParam("resumeId") Long resumeId,
            @RequestParam(value = "targetPosition", required = false) String targetPosition) {
        try {
            ResumeOptimizeResponse response = resumeOptimizeService.reOptimize(resumeId, targetPosition);
            return Result.ok(response);
        } catch (Exception e) {
            log.error("重新优化简历失败", e);
            return Result.fail("重新优化简历失败：" + e.getMessage());
        }
    }
}